#!/usr/bin/env python
# -*- coding: cp1252 -*-

import csv
import Util

class Inventory:
    Items = []

    def __init__(self):
        self.Items = []

    def __init__(self, filename):
        self.Items = []
        self.loadInventory(filename)

    def loadInventory(self, filename):

        fileinv = csv.reader(open(filename, 'rb'), delimiter=';')

        for row in fileinv:
            item = __InventoryItem__()
            item.Code = row[0]
            item.Description = row[1]

            if isinstance(row[2], int):
                item.ColorNumber = row[2]
            elif Util.is_integer(row[2]):
                item.ColorNumber = int(row[2])
            else:
                item.Color = row[2]
                item.ColorNumber = colorByName(row[2])

            item.Quantity = int(row[3])
            self.Items.append(item)

    def bricksQty(self):
        qty = 0

        for item in self.Items:
            qty += item.Quantity

        return qty

class __InventoryItem__:
    Code = ""
    Description = ""
    Color = ""
    ColorNumber = 0
    Quantity = 0

def colorByName(colorname):

    colortable = {'Light-Gray': 9, 'Dark-Gray': 10, 'Black': 11, 'Orange': 4}

    if colorname not in colortable:
        return None

    return colortable[colorname]
